from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from .models import PersonID,PersonSf
from django.urls import reverse
import re
# Create your views here.
def home(request):
    
    context = {}
    return render(request,'login/home.html',context)

def login(request):

    context = {}
    return render(request,'login/denglu.html',context)

def teacher_login(request):
    if request.method == 'GET':
        return HttpResponseRedirect(reverse('login:login'))
    elif request.method == 'POST':
        
        info = request.POST
        teacher_name = info['teacher_name']
        pswd = info['password']
        status = info['status']
        try:
            info_obj = PersonID.objects.get(person_name = teacher_name)
        except PersonID.DoesNotExist:
            context = {'message':'用户名不存在，请联系客服小姐姐'}
            return render(request,'login/denglu.html',context)
        else:
            if status == info_obj.sf.person_sf:
                if info_obj.person_pswd == None:
                    if pswd == info_obj.person_if[-6:]:
                        request.session['teacher_name'] = teacher_name
                        request.session['权限'] = '老师'
                        return HttpResponseRedirect(reverse('paper:teacher_index'))
                    else:
                        context = {'message':'您输入的账号或密码不正确！'}
                        return render(request,'login/denglu.html',context)
                elif pswd == info_obj.person_pswd:
                    request.session['teacher_name'] = teacher_name
                    request.session['权限'] = '老师'
                    return HttpResponseRedirect(reverse('paper:teacher_index'))
                else:
                    context = {'message':'您输入的账号或密码不正确！'}
                    return render(request,'login/denglu.html',context)
            else:
                context = {'message':'请到{}输入框登录！'.format(info_obj.sf.person_sf)}
                return render(request,'login/denglu.html',context)
def student_login(request):
    if request.method == 'POST':
        
        info = request.POST
        print(info)
        student_name = info['student_name']
        #print(type(username))
        pswd = info['password']
        print(info['status'])
        status = info['status']
    
        try:
            info_obj = PersonID.objects.get(person_name = student_name)
        except PersonID.DoesNotExist:
            context = {'message':'用户名不存在，请联系客服小姐姐！'}
            return render(request,'login/denglu.html',context)
            #return HttpResponse('用户名不存在，请联系客服小姐姐')
        else:
            if status == info_obj.sf.person_sf: 
                if info_obj.person_pswd == None:
                    if pswd == info_obj.person_id[-6:]:                    
                        request.session['student_name'] = student_name
                        request.session['权限'] = '学生'
                        return HttpResponseRedirect(reverse('paper:student_index'))                  
                    else:
                        context = {'message':'您输入的账号或密码不正确！'}
                        return render(request,'login/denglu.html',context)
                elif pswd == info_obj.person_pswd: 
                    request.session['student_name'] = student_name
                    request.session['权限'] = '学生'
                    return HttpResponseRedirect(reverse('paper:student_index'))
                else:
                    #return HttpResponseRedirect(reverse('login:login'))
                    context = {'message':'您输入的账号或密码不正确！'}
                    return render(request,'login/denglu.html',context)
            else:
                context = {'message':'请到{}输入框登录！'.format(info_obj.sf.person_sf)}
                return render(request,'login/denglu.html',context)
    elif request.method == 'GET':
        context = {'message':'请输入账号和密码'}
        return render(request,'login/denglu.html',context)
def alter_pswd(request):
    ses = request.session.items()
    print(ses)
    
    context = {}
    return render(request,'login/alter_pswd.html',context)
def alter_pswd_yunsuan(request):
    info = dict(request.POST)
    print(info)
    pswd_list = info['password']
    print(pswd_list[0])
    try:
        person_obj = PersonID.objects.get(person_name = info['username'][0])
    except PersonID.DoesNotExist:
        context = {'error':'账号不存在！'}
        return render(request,'login/alter_pswd.html',context)
        #return HttpResponse('账号不存在')
    else:
        if person_obj.person_pswd == pswd_list[0]:
            if pswd_list[1] == pswd_list[2] and 16 >= len(pswd_list[1]) >= 6:
                person_obj.person_pswd = pswd_list[1]
                person_obj.save()
                context = {'success':'密码修改成功！'}
                return render(request,'login/alter_pswd.html',context)
                #return HttpResponse('密码修改成功')
            else:
                context = {'error':'请检查你的密码，是否相同，或者符合长度规定！'}
                return render(request,'login/alter_pswd.html',context)
                #return HttpResponse('请检查你的密码，是否相同，或者符合长度规定')
        elif person_obj.person_pswd == None:
            if pswd_list[1] == pswd_list[2] and person_obj.person_id[-6:] == pswd_list[0]\
               and 16 >= len(pswd_list[1]) >= 6:
                person_obj.person_pswd = pswd_list[1]
                person_obj.save()
                context = {'success':'密码修改成功！'}
                return render(request,'login/alter_pswd.html',context)
                #return HttpResponse('密码修改成功')
            else:
                context = {'error':'请检查你的密码，是否相同，或者符合长度规定！'}
                return render(request,'login/alter_pswd.html',context)
                #return HttpResponse('请检查你的密码，是否相同，或者符合长度规定')
        else:
            context = {'error':'账号或密码输入有误！'}
            return render(request,'login/alter_pswd.html',context)
            #return HttpResponse('账号或密码输入有误！')
def register(request):
    sf_all = PersonSf.objects.all()
    sf_list = []
    for sf_obj in sf_all:
        sf_list.append(sf_obj.person_sf)
    #注册功能缺失：将保存的身份信息传到模板里，然后让用户选择身份并注册
    context = {'sf_list':sf_list}
    return render(request,'login/register.html',context)

def hasSpecial(x):
    return bool(re.search(r'[A-Za-z0-9_]',x))

def register_save(request):
    info = dict(request.POST)
    username = info['username'][0]
    user_id = info['user_id'][0]
    pswd_list = info['password']
    person_list_id = PersonSf.objects.get(person_sf = '学生').personid_set.values('person_id')
    person_list_name = PersonSf.objects.get(person_sf = '学生').personid_set.values('person_name')
    
    name_list = [person_dict['person_name'] for person_dict in person_list_name]
    id_list = [person_dict['person_id'] for person_dict in person_list_id]    

    if hasSpecial(username) == True:
        context = {'error':'账号中不能包含数字，字符或特殊符号！'}
        return render(request,'login/register.html',context)
    elif hasSpecial(username) == False:
        if username in name_list:
            context = {'error':'此用户名已存在，请重新输入！'}
            return render(request,'login/register.html',context)
        else:
            if 18 >= len(user_id) >= 16:
                if user_id in id_list:
                    context = {'error':'此身份证号已存在，请重新输入！'}
                    return render(request,'login/register.html',context)
                else:
                    if pswd_list[0] == pswd_list[1] and 16 >= len(pswd_list[0]) >= 6:
                        the_sf = PersonSf.objects.get(person_sf = '学生')
                        PersonID.objects.create(person_name = username,\
                                                person_id = user_id,person_pswd = pswd_list[0],\
                                                sf = the_sf)
                        context = {'success':'恭喜你注册成功，请返回登录！'}
                        return render(request,'login/register.html',context)
                    else:
                        context = {'error':'请检查你的密码，是否相同，或者符合长度规定！'}
                        return render(request,'login/register.html',context)
            else:
                context = {'error':'身份证号码必须在16位到18位之间！'}
                return render(request,'login/register.html',context)
    '''
    if any(char.isdigit() for char in username) == True:
        context = {'error':'账号中不能包含数字，字符或特殊符号！'}
        return render(request,'login/register.html',context)
    '''   







        
